/**
 * 管理系统公共JS
 * 包含各种页面共用的功能和方法
 */

// 侧边栏导航初始化
function initSidebarNavigation() {
    // 获取所有带有子菜单的导航项
    const navItems = document.querySelectorAll('.nav-item.has-submenu');

    // 从本地存储加载导航菜单状态
    const menuStates = JSON.parse(localStorage.getItem('menuStates')) || {};

    // 初始化导航菜单状态
    navItems.forEach(item => {
        const submenu = item.querySelector('.submenu');
        const toggle = item.querySelector('.submenu-toggle');
        const itemId = item.getAttribute('data-id') || Array.from(navItems).indexOf(item).toString();

        // 如果本地存储中有该菜单的状态，则应用它
        if (menuStates[itemId] !== undefined) {
            if (menuStates[itemId]) {
                submenu.classList.add('active');
                toggle.classList.remove('fa-angle-down');
                toggle.classList.add('fa-angle-up');
            } else {
                submenu.classList.remove('active');
                toggle.classList.remove('fa-angle-up');
                toggle.classList.add('fa-angle-down');
            }
        }

        // 为导航项添加点击事件
        item.querySelector('.nav-link').addEventListener('click', function (e) {
            e.preventDefault();

            const isExpanded = submenu.style.display === 'block';

            // 切换图标,切换子菜单显示状态
            if (isExpanded) {
                submenu.classList.remove('active');
                toggle.classList.remove('fa-angle-up');
                toggle.classList.add('fa-angle-down');
            } else {
                submenu.classList.add('active');
                toggle.classList.remove('fa-angle-down');
                toggle.classList.add('fa-angle-up');
            }
        });
    });
}

function clearForm() {
    const formElement = document.getElementById('queryForm')
    const inputs = formElement.elements;
    for (let i = 0; i < inputs.length; i++) {
        const input = inputs[i];
        switch (input.type) {
            case 'text':
            case 'password':
            case 'textarea':
            case 'email':
            case 'number':
            case 'date':
                input.value = '';
                break;
            case 'checkbox':
            case 'radio':
                input.checked = false;
                break;
            case 'select-one':
            case 'select-multiple':
                input.selectedIndex = 0;
                break;
            case 'file':
                input.value = '';
                break;
        }
    }
}

function previewImage(url, title) {
    document.getElementById('previewImg').src = url;
    document.getElementById('previewImg').alt = title;
    document.getElementById('previewImg').classList.remove('hidden');

    document.getElementById('previewTitle').textContent = title;
    document.getElementById('previewModal').classList.remove('hidden');
    document.body.style.overflow = 'hidden';
}

function previewVideo(url, title) {
    document.getElementById('previewVideo').src = url;
    document.getElementById('previewVideo').alt = title;
    document.getElementById('previewVideo').classList.remove('hidden');

    document.getElementById('previewTitle').textContent = title;
    document.getElementById('previewModal').classList.remove('hidden');
    document.body.style.overflow = 'hidden';
}

function closePreview() {
    document.getElementById('previewImg').classList.add('hidden');
    document.getElementById('previewVideo').classList.add('hidden');
    document.getElementById('previewModal').classList.add('hidden');
    document.body.style.overflow = 'auto';
}

function previewShow(file_type, file_name, title) {
    axios.get('/public/get_preview_url?file_name=' + file_name, {},
        {
            headers: {
                'X-CSRFToken': document.getElementById('csrf_token').value,
                'Content-Type': 'application/json'
            }
        }
    ).then(response => {
        if (response.data.status === "success") {
            if (file_type === 'image') {
                previewImage(response.data.data.path, title);
            } else {
                previewVideo(response.data.data.path, title);
            }
        }
    }).catch(error => {
        console.error('error:', error);
    });
}